/**
 * 获取最近的三条发布信息
 * @param data
 */
function getRecentPost(data){
	for (var i = 0; i <data.length; i++) {
		var	recent=$("<li><a href='single.html?id="+data[i][0]+"'>"+data[i][1]+"</a></li>");
		recent.appendTo($("#recentpost"));
	}
}

/**
 * 
 * 获取url参数
 * @param name
 * @returns
 */
function GetQueryString(name)
{
 var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
 var r = window.location.search.substr(1).match(reg);
 if(r!=null)return  unescape(r[2]); return null;
}

/**
 * 去除所有html标签
 * @param str
 * @returns
 */

function delHtmlTag(str){
	  return str.replace(/<[^>]+>/g,"");//去掉所有的html标记
	 }

/**
 * 展示所有数据
 * @param data
 */

function showBlogList(data){
	if(data == null){
		return;
	}
	for (var i = 0; i < data.length; i++) {
		var article = $("<article class='post post-1'></article>");
		article.appendTo($("#bloglist"));
		var header =$("<header class='entry-header'></header>");
		header.appendTo(article);
		var h1=$("<h1 class='entry-title'></h1>");
		var title = $("<a href='single.html?id="+data[i].id+"'>"+data[i].title+"</a>");
		title.appendTo(h1);
		h1.appendTo(header);
		var metaDiv=$("<div class='entry-meta'></div>");
		metaDiv.appendTo(header);
		var kind = $("<span class='post-category'><a>" + data[i].kind
				+ "<a/></span>");
		kind.appendTo(metaDiv);
		var date=$("<span class='post-date'>" 
				+ "</span>");
		var pubdate = $("<a><time class='entry-date' datetime='"+data[i].pubdate+"''>"
				+ data[i].pubdate + "</time></a>");
		pubdate.appendTo(date);
		date.appendTo(metaDiv);

		var author = $("<span class='post-author'><a>" 
				+ data[i].author + "<a></span>");
		author.appendTo(metaDiv);
		var Comments=$("<span class='comments-link'><a>" 
				+data[i].messageCount+"条评论<a></span>");
		Comments.appendTo(metaDiv);
		//正文
		var mainText = $("<div class='list-content'>"
				+ delHtmlTag(data[i].mainText) + "</div>");
		var contentDiv=$("<div class='entry-content clearfix '></div>");
		contentDiv.appendTo(article);
		mainText.appendTo(contentDiv);
		var readDiv=$("<div class='read-more cl-effect-14'></div>");
		readDiv.appendTo(contentDiv);
		var link= $("<a href='single.html?id="+data[i].id+"'>详情</a>");
		link.appendTo(readDiv);
	};
}


function setPageParam(data,kind) {
	var nextPager,
		prePager,
		pager;
	 pager = $("<ul class='pager' id='pager'></ul><br>");
	 pager.appendTo($("#bloglist"));
	if(data.hasNextPage){
		nextPager =$("<li class='next'><a href=category.html?kind="+kind+"&pageCount="+(data.currentPage+1)+" >下一页 &rarr;</a></li>");
	}else{
		nextPager =$("<li class='next disabled'><a >下一页 &rarr;</a></li>");
	}
	
	if(data.hasPreviousPage){
		prePager =$("<li class='previous'><a href=category.html?kind="+kind+"&pageCount="+(data.currentPage-1)+" >&larr; 上一页</a></li>");
	}else{
		prePager =$("<li class='previous disabled'><a >&larr; 上一页</a></li>");
	}
	nextPager.appendTo(pager);
	prePager.appendTo(pager);
	
}

/**
 * 显示搜索结果
 * 
 */
function showSearchResult(kind) {
	var pageCount= GetQueryString("pageCount") ;
	var limit = 4;
	$.ajax({
		type: "GET",
		url: "blog_searchBlogArticleByKind.action",
		data: "kind="+kind+"&page="+(pageCount == undefined?1:pageCount)+"&limit="+limit,
		dataType: 'json',
		success: function(result) {
			if(result.status != 200){
				var article = $("<article class='post post-1 lead'>呜呜呜~木有搜索到内容//(ㄒoㄒ)//</article>");
				article.appendTo($("#bloglist"));
				return;
			}
			showBlogList(result.data.list);
			
			if(result.data.allRow>limit){
				setPageParam(result.data,kind);
			}
		}
	});
}
/**
 * 
 */
function doSubmitForm() {
    var form = document.getElementById('ipt-content');
   // showSearchResult(form.value);
    window.location.href="category.html?kind="+form.value;
}

	
$(function(){
	
	var kind=GetQueryString("kind");
	if(kind !=null && kind.toString().length>0)
	{	
		showSearchResult(kind);
		$.ajax({
			type: "POST",
			url: "blog_requestSomeBlogByAttr.action",
			data: {startId:0,limit:3,attr:"title"},
			dataType: 'json',
			success: function(datas) {
				var data = datas.data;
				getRecentPost(data);
			}
		});
	}
});